home *** CD-ROM | disk | FTP | other *** search
/ Team Palmtops 7 / Palmtops_numero07.iso / WinCE / SDKWindowsCE / HandHeldPCPro30 / sdk.exe / Jupiter SDK / data1.cab / MFC / src / filest.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1999-02-19  |  7.9 KB  |  295 lines

  1. // This is a part of the Microsoft Foundation Classes C++ library.
  2. // Copyright (C) 1992-1998 Microsoft Corporation
  3. // All rights reserved.
  4. //
  5. // This source code is only intended as a supplement to the
  6. // Microsoft Foundation Classes Reference and related
  7. // electronic documentation provided with the library.
  8. // See these sources for detailed information regarding the
  9. // Microsoft Foundation Classes product.
  10.  
  11. #include "stdafx.h"
  12. #ifndef _INC_ERRNO
  13. #include <errno.h>
  14. #endif
  15. #ifndef _INC_IO
  16. #include <io.h>
  17. #endif
  18. #ifndef _INC_TYPES
  19. #include <sys\types.h>
  20. #endif
  21. #ifndef _INC_STAT
  22. #include <sys\stat.h>
  23. #endif
  24.  
  25. #ifdef AFX_CORE1_SEG
  26. #pragma code_seg(AFX_CORE1_SEG)
  27. #endif
  28.  
  29. #ifdef _DEBUG
  30. #undef THIS_FILE
  31. static char THIS_FILE[] = __FILE__;
  32. #endif
  33.  
  34. #define new DEBUG_NEW
  35.  
  36. ////////////////////////////////////////////////////////////////////////////
  37. // Status information for all file classes
  38. // In this file so everyone doesn't get the CTime package
  39.  
  40. /////////////////////////////////////////////////////////////////////////////
  41. // CFileStatus diagnostics
  42.  
  43. #ifdef _DEBUG
  44. void CFileStatus::Dump(CDumpContext& dc) const
  45. {
  46.     dc << "a CFileStatus at " << (void*)this;
  47.  
  48.     dc << "\nm_ctime = " << m_ctime;
  49.     dc << "\nm_mtime = " << m_mtime;
  50.     dc << "\nm_atime = " << m_atime;
  51.     dc << "\nm_size = " << m_size;
  52.     dc << "\nm_attribute = " << m_attribute;
  53.     dc << "\nm_szFullName = " << m_szFullName;
  54.  
  55.     dc << "\n";
  56. }
  57. #endif
  58.  
  59. /////////////////////////////////////////////////////////////////////////////
  60. // CFile name handlers
  61.  
  62. CString CFile::GetFileName() const
  63. {
  64.     ASSERT_VALID(this);
  65.  
  66.     CFileStatus status;
  67.     GetStatus(status);
  68.     CString strResult;
  69.     AfxGetFileName(status.m_szFullName, strResult.GetBuffer(_MAX_FNAME),
  70.         _MAX_FNAME);
  71.     strResult.ReleaseBuffer();
  72.     return strResult;
  73. }
  74.  
  75. CString CFile::GetFileTitle() const
  76. {
  77.     ASSERT_VALID(this);
  78.  
  79.     CFileStatus status;
  80.     GetStatus(status);
  81.     CString strResult;
  82.     AfxGetFileTitle(status.m_szFullName, strResult.GetBuffer(_MAX_FNAME),
  83.         _MAX_FNAME);
  84.     strResult.ReleaseBuffer();
  85.     return strResult;
  86. }
  87.  
  88. CString CFile::GetFilePath() const
  89. {
  90.     ASSERT_VALID(this);
  91.  
  92.     CFileStatus status;
  93.     GetStatus(status);
  94.     return status.m_szFullName;
  95. }
  96.  
  97. /////////////////////////////////////////////////////////////////////////////
  98. // CFile Status implementation
  99.  
  100. BOOL CFile::GetStatus(CFileStatus& rStatus) const
  101. {
  102.     ASSERT_VALID(this);
  103.  
  104.     memset(&rStatus, 0, sizeof(CFileStatus));
  105.  
  106.     // copy file name from cached m_strFileName
  107.     lstrcpyn(rStatus.m_szFullName, m_strFileName,
  108.         _countof(rStatus.m_szFullName));
  109.  
  110.     if (m_hFile != hFileNull)
  111.     {
  112.         // get time current file size
  113.         FILETIME ftCreate, ftAccess, ftModify;
  114.         if (!::GetFileTime((HANDLE)m_hFile, &ftCreate, &ftAccess, &ftModify))
  115.             return FALSE;
  116.  
  117.         if ((rStatus.m_size = ::GetFileSize((HANDLE)m_hFile, NULL)) == (DWORD)-1L)
  118.             return FALSE;
  119.  
  120.         if (m_strFileName.IsEmpty())
  121.             rStatus.m_attribute = 0;
  122.         else
  123.         {
  124.             DWORD dwAttribute = ::GetFileAttributes(m_strFileName);
  125.  
  126.             // don't return an error for this because previous versions of MFC didn't
  127.             if (dwAttribute == 0xFFFFFFFF)
  128.                 rStatus.m_attribute = 0;
  129.             else
  130.             {
  131.                 rStatus.m_attribute = (BYTE) dwAttribute;
  132. #ifdef _DEBUG
  133.                 // MFC BUG: m_attribute is only a BYTE wide
  134.                 if (dwAttribute & ~0xFF)
  135.                     TRACE0("Warning: CFile::GetStatus() returns m_attribute without high-order flags.\n");
  136. #endif
  137.             }
  138.         }
  139.  
  140.         // convert times as appropriate
  141.         rStatus.m_ctime = CTime(ftCreate);
  142.         rStatus.m_atime = CTime(ftAccess);
  143.         rStatus.m_mtime = CTime(ftModify);
  144.  
  145.         if (rStatus.m_ctime.GetTime() == 0)
  146.             rStatus.m_ctime = rStatus.m_mtime;
  147.  
  148.         if (rStatus.m_atime.GetTime() == 0)
  149.             rStatus.m_atime = rStatus.m_mtime;
  150.     }
  151.     return TRUE;
  152. }
  153.  
  154. BOOL PASCAL CFile::GetStatus(LPCTSTR lpszFileName, CFileStatus& rStatus)
  155. {
  156.     // attempt to fully qualify path first
  157.     if (!AfxFullPath(rStatus.m_szFullName, lpszFileName))
  158.     {
  159.         rStatus.m_szFullName[0] = '\0';
  160.         return FALSE;
  161.     }
  162.  
  163.     WIN32_FIND_DATA findFileData;
  164.     HANDLE hFind = FindFirstFile((LPTSTR)lpszFileName, &findFileData);
  165.     if (hFind == INVALID_HANDLE_VALUE)
  166.         return FALSE;
  167.     VERIFY(FindClose(hFind));
  168.  
  169.     // strip attribute of NORMAL bit, our API doesn't have a "normal" bit.
  170.     rStatus.m_attribute = (BYTE)
  171.         (findFileData.dwFileAttributes & ~FILE_ATTRIBUTE_NORMAL);
  172.  
  173.     // get just the low DWORD of the file size
  174.     ASSERT(findFileData.nFileSizeHigh == 0);
  175.     rStatus.m_size = (LONG)findFileData.nFileSizeLow;
  176.  
  177.     // convert times as appropriate
  178.     rStatus.m_ctime = CTime(findFileData.ftCreationTime);
  179.     rStatus.m_atime = CTime(findFileData.ftLastAccessTime);
  180.     rStatus.m_mtime = CTime(findFileData.ftLastWriteTime);
  181.  
  182.     if (rStatus.m_ctime.GetTime() == 0)
  183.         rStatus.m_ctime = rStatus.m_mtime;
  184.  
  185.     if (rStatus.m_atime.GetTime() == 0)
  186.         rStatus.m_atime = rStatus.m_mtime;
  187.  
  188.     return TRUE;
  189. }
  190.  
  191. void AFX_CDECL AfxTimeToFileTime(const CTime& time, LPFILETIME pFileTime)
  192. {
  193.     SYSTEMTIME sysTime;
  194.     sysTime.wYear = (WORD)time.GetYear();
  195.     sysTime.wMonth = (WORD)time.GetMonth();
  196.     sysTime.wDay = (WORD)time.GetDay();
  197.     sysTime.wHour = (WORD)time.GetHour();
  198.     sysTime.wMinute = (WORD)time.GetMinute();
  199.     sysTime.wSecond = (WORD)time.GetSecond();
  200.     sysTime.wMilliseconds = 0;
  201.  
  202.     // convert system time to local file time
  203.     FILETIME localTime;
  204.     if (!SystemTimeToFileTime((LPSYSTEMTIME)&sysTime, &localTime))
  205.         CFileException::ThrowOsError((LONG)::GetLastError());
  206.  
  207.     // convert local file time to UTC file time
  208.     if (!LocalFileTimeToFileTime(&localTime, pFileTime))
  209.         CFileException::ThrowOsError((LONG)::GetLastError());
  210. }
  211.  
  212. void PASCAL CFile::SetStatus(LPCTSTR lpszFileName, const CFileStatus& status)
  213. {
  214.     DWORD wAttr;
  215.     FILETIME creationTime;
  216.     FILETIME lastAccessTime;
  217.     FILETIME lastWriteTime;
  218.     LPFILETIME lpCreationTime = NULL;
  219.     LPFILETIME lpLastAccessTime = NULL;
  220.     LPFILETIME lpLastWriteTime = NULL;
  221.  
  222.     if ((wAttr = GetFileAttributes((LPTSTR)lpszFileName)) == (DWORD)-1L)
  223.         CFileException::ThrowOsError((LONG)GetLastError());
  224.  
  225.     if ((DWORD)status.m_attribute != wAttr && (wAttr & readOnly))
  226.     {
  227.         // Set file attribute, only if currently readonly.
  228.         // This way we will be able to modify the time assuming the
  229.         // caller changed the file from readonly.
  230.  
  231.         if (!SetFileAttributes((LPTSTR)lpszFileName, (DWORD)status.m_attribute))
  232.             CFileException::ThrowOsError((LONG)GetLastError());
  233.     }
  234.  
  235.     // last modification time
  236.     if (status.m_mtime.GetTime() != 0)
  237.     {
  238.         AfxTimeToFileTime(status.m_mtime, &lastWriteTime);
  239.         lpLastWriteTime = &lastWriteTime;
  240.  
  241.         // last access time
  242.         if (status.m_atime.GetTime() != 0)
  243.         {
  244.             AfxTimeToFileTime(status.m_atime, &lastAccessTime);
  245.             lpLastAccessTime = &lastAccessTime;
  246.         }
  247.  
  248.         // create time
  249.         if (status.m_ctime.GetTime() != 0)
  250.         {
  251.             AfxTimeToFileTime(status.m_ctime, &creationTime);
  252.             lpCreationTime = &creationTime;
  253.         }
  254.  
  255.         HANDLE hFile = ::CreateFile(lpszFileName, GENERIC_READ|GENERIC_WRITE,
  256.             FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL,
  257.             NULL);
  258.  
  259.         if (hFile == INVALID_HANDLE_VALUE)
  260.             CFileException::ThrowOsError((LONG)::GetLastError());
  261.  
  262.         if (!SetFileTime((HANDLE)hFile, lpCreationTime, lpLastAccessTime, lpLastWriteTime))
  263.             CFileException::ThrowOsError((LONG)::GetLastError());
  264.  
  265.         if (!::CloseHandle(hFile))
  266.             CFileException::ThrowOsError((LONG)::GetLastError());
  267.     }
  268.  
  269.     if ((DWORD)status.m_attribute != wAttr && !(wAttr & readOnly))
  270.     {
  271.         if (!SetFileAttributes((LPTSTR)lpszFileName, (DWORD)status.m_attribute))
  272.             CFileException::ThrowOsError((LONG)GetLastError());
  273.     }
  274. }
  275.  
  276. #if !defined(_WIN32_WCE)
  277. ///////////////////////////////////////////////////////////////////////////////
  278. // CMemFile::GetStatus implementation
  279.  
  280. BOOL CMemFile::GetStatus(CFileStatus& rStatus) const
  281. {
  282.     ASSERT_VALID(this);
  283.  
  284.     rStatus.m_ctime = 0;
  285.     rStatus.m_mtime = 0;
  286.     rStatus.m_atime = 0;
  287.     rStatus.m_size = m_nFileSize;
  288.     rStatus.m_attribute = normal;
  289.     rStatus.m_szFullName[0] = '\0';
  290.     return TRUE;
  291. }
  292.  
  293. /////////////////////////////////////////////////////////////////////////////
  294. #endif // _WIN32_WCE
  295.